#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：8.作业阿里.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-29 16:30
'''
import pymysql
import requests


class AliWork:
    def __init__(self):
        self.url = "https://talent.taotian.com/position/search?_csrf=f35ef899-efd4-4838-a345-3945cb0aaadd"
        self.cookies = {
            'XSRF-TOKEN': 'f35ef899-efd4-4838-a345-3945cb0aaadd',
        }
        self.headers = {
            # 'referer': 'https://talent.taotian.com/off-campus/position-list?lang=zh',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
        }
        self.json_data = {
            'channel': 'group_official_site',
            'language': 'zh',
            'batchId': '',
            'categories': '',
            'deptCodes': [],
            'key': '',
            'pageIndex': 1,
            'pageSize': 10,
            'regions': '',
            'subCategories': '',
            'shareType': '',
            'shareId': '',
            'myReferralShareCode': '',
        }

        self.db = pymysql.Connect(host="localhost", user="root", password="123456", db="py_spider")
        self.cursor = self.db.cursor()

    def __del__(self):
        print("数据库连接关闭")
        self.cursor.close()
        self.db.close()

    def create_table(self):
        sql = """
            create table if not exists al_work(
                id int primary key auto_increment,
                work_name varchar(100) not null,
                country_name varchar(50),
                job_func text,
                work_desc text
            );
        """

        try:
            self.cursor.execute(sql)
            print("创建表成功")
        except Exception as e:
            print("创建表失败：", e)

    def get_work_info(self):
        for page in range(1, 5):
            self.json_data['pageIndex'] = page
            response = requests.post(self.url, json=self.json_data, headers=self.headers, cookies=self.cookies).json()
            yield response['content']['datas']

    def insert_work_info(self, *args):
        sql = """
            insert into al_work values (%s, %s, %s, %s, %s);
        """
        try:
            self.cursor.execute(sql, args)
            self.db.commit()
            print("数据插入成功")
        except Exception as e:
            print("数据插入失败：", e)
            self.db.rollback()

    def main(self):
        self.create_table()
        resul_generator = self.get_work_info()
        work_id = 0
        for resul_list in resul_generator:
            for resuls in resul_list:
                work_name = resuls["name"]
                job_func = resuls["requirement"]
                work_desc = resuls["description"]
                country_name = resuls["workLocations"][0]
                self.insert_work_info(work_id, work_name, country_name, job_func, work_desc)


if __name__ == '__main__':
    aliwork = AliWork()
    aliwork.main()